package com.example.a22310.myapplication.Activity;

//红色的R解决不了
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.example.a22310.myapplication.ViewModel.Order.ActivityOrderViewModel;
import com.example.a22310.myapplication.ViewModel.Order.OrderViewModel;
import com.example.a22310.myapplication.ViewModel.Order.TrainOrderViewModel;
import com.example.a22310.myapplication.databinding.ActivityOrderBinding;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

public class OrderActivity extends AppCompatActivity {
    private String FILENAME = "yuejian'data";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityOrderBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_order);

        new OrderViewModel(this,binding,getUserid(),1);
        new TrainOrderViewModel(this,binding,getUserid());
        new ActivityOrderViewModel(this,binding,getUserid());


        System.out.println("ppppppppppp"+sHA1(OrderActivity.this));
    }


    private int  getUserid(){
        int newsNumber;
        SharedPreferences sharedPreferences = getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
        newsNumber = sharedPreferences.getInt("userId",0);
        return newsNumber;
    }


    public static String sHA1(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), PackageManager.GET_SIGNATURES);
            byte[] cert = info.signatures[0].toByteArray();
            MessageDigest md = MessageDigest.getInstance("SHA1");
            byte[] publicKey = md.digest(cert);
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < publicKey.length; i++) {
                String appendString = Integer.toHexString(0xFF & publicKey[i])
                        .toUpperCase(Locale.US);
                if (appendString.length() == 1)
                    hexString.append("0");
                hexString.append(appendString);
                hexString.append(":");
            }
            String result = hexString.toString();
            return result.substring(0, result.length()-1);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}
